﻿using System.Windows;
using System.Windows.Media;

namespace Sample
{
    public static class ExtensionMethods
    {
        // See http://iimaginec.wordpress.com/2011/05/13/wpf-find-visual-tree-ancestor-by-type-the-better-way-of-getparent/
        public static T FindAncestor<T>(this DependencyObject obj)
            where T : DependencyObject
        {
            while (obj != null)
            {
                var o = obj as T;
                if (o != null)
                {
                    return o;
                }
                obj = VisualTreeHelper.GetParent(obj);
            }
            return null;
        }
    }
}
